package helperClassSites;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import utils.Utils;

public class CreateGames {

	public static void CreateGamesFiles(String path, int xlsSheet){
		try {
			Workbook workbook = Workbook.getWorkbook(new File(
					"C:\\Documents and Settings\\Administrator\\My Documents\\Dropbox\\CTA\\XLS\\CTA.xls"));
			Sheet sheet = workbook.getSheet(xlsSheet);
			Cell[] count = sheet.getColumn(0);
			Sheet other = workbook.getSheet(8);
			Cell codeCell = other.getCell(0, 0);
			String code = codeCell.getContents();
			
			
			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(9, i);
				String sitename = siteNameCell.getContents();
				File replacedFile = new File(path + sitename + ".html");
				replacedFile.delete();
				FileWriter fstream = new FileWriter(path + sitename + ".html");
				BufferedWriter textFile = new BufferedWriter(fstream);

				textFile.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html> \n");
				textFile.write("<head> \n");
				textFile.write("  <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /> \n");
				textFile.write("<style type=\"text/css\">\n");
				textFile.write("div.Outer {\n");
				textFile.write("	width: 100%;\n");
				textFile.write("height: 100%;\n");
				textFile.write("border: 0px;\n");
				textFile.write("position: absolute;\n");
				textFile.write("clear: both;\n");
				textFile.write("}\n");
				textFile.write("div.InnerLeft {\n");
				textFile.write("width: 92%;\n");
				textFile.write("height: 100%;\n");
				textFile.write("position: relative;\n");
				textFile.write("float: left;\n");
				textFile.write("}\n");
				textFile.write("div.InnerRight {\n");
				textFile.write("allign: right;\n");
				textFile.write("width: 8%;\n");
				textFile.write("text-align: right;\n");
				textFile.write("position: relative;\n");
				textFile.write("float: right;\n");
				textFile.write("}\n");
				textFile.write("</style>\n");
		        Cell passNameCell = sheet.getCell(4, i);
		        String passNameEN = passNameCell.getContents();
		        String replaced = Utils.replaceUmlautsDeutsch(passNameEN);
				textFile.write("  <meta name=\"Description\" content=\"3D game of the " + replaced + ". This game is based on google earth and let you explore the " + replaced +" by playing around. There are around 480 passes on www.cyclingthealps.com. Come and have a look.\" />\n");
				textFile.write("<!--\n");
				textFile.write("Copyright 2008 Google Inc.\n");
				textFile.write(" \n");
				textFile.write("Licensed under the Apache License, Version 2.0 (the \"License\");\n");
				textFile.write("you may not use this file except in compliance with the License.\n");
				textFile.write("You may obtain a copy of the License at\n");
				textFile.write(" \n");
				textFile.write("     http://www.apache.org/licenses/LICENSE-2.0\n");
				textFile.write(" \n");
				textFile.write("Unless required by applicable law or agreed to in writing, software\n");
				textFile.write("distributed under the License is distributed on an \"AS IS\" BASIS,\n");
				textFile.write("WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n");
				textFile.write("See the License for the specific language governing permissions and\n");
				textFile.write("limitations under the License.\n");
				textFile.write("--> \n");
				textFile.write("  <title>" + replaced + " 3D Game</title> \n");
				textFile.write("<script type=\"text/javascript\">var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));</script>");
				textFile.write("    \n");
				textFile.write("<script type=\"text/javascript\">  var _gaq = _gaq || [];  _gaq.push(['_setAccount', 'UA-13084846-1']);  _gaq.push(['_trackPageview']);  (function() {    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  })();</script>");
				textFile.write("    \n");
				textFile.write("  <link rel=stylesheet href='../../game/lib/milktruck.css' type='text/css' /> \n");
				
				textFile.write("  <script type=\"text/javascript\"> \n");
				textFile.write("  var googleMapsApiKeys = []; \n");
				textFile.write("  googleMapsApiKeys['http://cta.wegewijs.info/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRTBicl7mDxxQ0ZVLXxnkdyYsvjN2xRD0jVY52fdXUpjz9rANIBJiWPV0w';\n");
				textFile.write("  googleMapsApiKeys['http://www.wegewijs.info/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRT-xS-a97Jv2dVLwJNVV1w_TZi6pBTQpY11tZ1Y4fardIpuI2sHtbfrcQ';\n");
				textFile.write("  googleMapsApiKeys['http://www.cyclingthealps.com/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRRu0hKdmJtwqUpy0N8zchdihiwGgRT7yprgzTDNAnphI0d4JwZ71whrhA';\n");
				textFile.write("  googleMapsApiKeys['http://cta.cyclingthealps.com/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRRxWkNdpiRta-ntjL1r0v29A2_XuBQriWGZ66izn5yupLMismdpENe4MA';\n");
				textFile.write("  googleMapsApiKeys['http://alpspasses.appspot.com/'] = 'ABQIAAAAM4pWLS64lWH2T51CW5FpkRTn0IpzAJXD01_fqfQFNim4xN6saxRtEL98s25b9oDrAP9cOhoAO2sfMQ';\n");
				textFile.write( code + " \n");
				textFile.write(" </script>\n");
				
//				textFile.write("  <script src=\"http://www.google.com/jsapi?key=ABQIAAAAM4pWLS64lWH2T51CW5FpkRRu0hKdmJtwqUpy0N8zchdihiwGgRT7yprgzTDNAnphI0d4JwZ71whrhA\"></script> \n");
				textFile.write("  <script type=\"text/javascript\" src=\"../../game/lib/math3d.js\"></script> \n");
				textFile.write("  <script type=\"text/javascript\" src=\"../../game/lib/keyboard-focus-hack.js\"></script> \n");
				textFile.write("  <script type=\"text/javascript\" src=\"../../game/lib/kmldomwalk.js\"></script> \n");
				textFile.write("  <script type=\"text/javascript\" src=\"../../game/lib/milktruck.js\"></script> \n");
				textFile.write("  <script type=\"text/javascript\"> \n");
				textFile.write("google.load(\"earth\", \"1\");\n");
				textFile.write("google.load(\"maps\", \"2\");\n");
				textFile.write(" \n");
				textFile.write("var ge = null;\n");
				textFile.write("var geocoder;\n");
				textFile.write("var truck;\n");
				textFile.write("\n");
				Cell latlngCellX = sheet.getCell(1, i);
				String latlngX = latlngCellX.getContents();
				Cell latlngCellY = sheet.getCell(2, i);
				String latlngY = latlngCellY.getContents();
				textFile.write("var lat = " + latlngX + ";\n");
				textFile.write("var lon = " + latlngY + ";\n");
				Cell headingCellX = sheet.getCell(26, i);
				String heading = headingCellX.getContents();
				textFile.write("var heading = " + heading +";\n");
				Cell linkCell = sheet.getCell(16, i);
				String link = linkCell.getContents();
				textFile.write("var link = '" + link + "';\n");
				textFile.write("\n");
				textFile.write("function el(e) { return document.getElementById(e); }\n");
				textFile.write(" \n");
				textFile.write("function Sample(description, url) {\n");
				textFile.write("  this.description = description;\n");
				textFile.write("  this.url = url;\n");
				textFile.write("  return this;\n");
				textFile.write("}\n");
				textFile.write(" \n");
				textFile.write("var samples = [];\n");
				textFile.write(" \n");
				textFile.write("function init() {\n");
				textFile.write("  geocoder = new GClientGeocoder();\n");
				textFile.write(" \n");
				textFile.write("  init3D();\n");
				textFile.write("\n");
				textFile.write("}\n");
				textFile.write("\n");
				textFile.write("\n");
				textFile.write(" \n");
				textFile.write("function initCallback(object) {\n");
				textFile.write("  ge = object;\n");
				textFile.write("  ge.getWindow().setVisibility(true);\n");
				textFile.write("  ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, true);\n");
				textFile.write("  ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT);\n");
				textFile.write("										\n");
				textFile.write("  truck = new Truck(lat, lon, heading,link);\n");
				textFile.write("  keyboardFocusHack(ge);\n");
				textFile.write("}\n");
				textFile.write(" \n");
				textFile.write("function failureCallback(err) {\n");
				textFile.write("  /***\n");
				textFile.write("   * This function will be called if plugin fails to load, in case\n");
				textFile.write("   * you need to handle that error condition.\n");
				textFile.write("   ***/\n");
				textFile.write("}\n");
				textFile.write(" \n");
				textFile.write("function init3D() {\n");
				textFile.write("  google.earth.createInstance(\"map3d\", initCallback, failureCallback);\n");
				textFile.write("}\n");
				textFile.write(" \n");
				textFile.write("function submitLocation() {\n");
				textFile.write("  doGeocode(el('address').value);\n");
				textFile.write("}\n");
				textFile.write(" \n");
				textFile.write("function doGeocode(address) {\n");
				textFile.write("  geocoder.getLatLng(address, function(point) {\n");
				textFile.write("    if (point) {\n");
				textFile.write("      if (ge != null && truck != null) {\n");
				textFile.write("        truck.teleportTo(point.y, point.x);\n");
				textFile.write("      }\n");
				textFile.write("    }\n");
				textFile.write("  });\n");
				textFile.write("}\n");
				textFile.write("\n");
				textFile.write("\n");
				textFile.write("  </script> \n");
				textFile.write("</head> \n");
				textFile.write("<body onload='init()' onunload=\"GUnload()\" onKeyDown=\"return keyDown(event);\" onKeyUp=\"return keyUp(event);\" style= \"padding: 0; overflow: hidden; margin: 0; background: #fafad7;\"> \n");
				textFile.write("  <div align=\"right\">\n");
				Cell hyperlinkNameCell = sheet.getCell(35, i);
				String hyperlink = hyperlinkNameCell.getContents();
				textFile.write(" <a href=\"http://www.cyclingthealps.com/#!" + hyperlink +"\"> <img src=\"http://www.cyclingthealps.com/images/close.png\"> </a>\n");
				textFile.write("<div class=\"Outer\">	<div id=\"map3d\" class=\"InnerLeft\"></div>\n");
				textFile.write("<div class=\"InnerRight\">\n");
				textFile.write("<script type=\"text/javascript\"><!--\n");
				textFile.write("google_ad_client = \"ca-pub-3877595695892478\"; /* game */ google_ad_slot = \"2781798536\"; google_ad_width = 120; google_ad_height = 600;\n");
				textFile.write("//--> </script> <script type=\"text/javascript\" src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\"> </script> </div></div>\n");
				textFile.write("</body> \n");
				textFile.write("</html> \n");
				textFile.close();
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
}
